package edu.strategy;

import java.io.Serializable;

import robocode.AdvancedRobot;
import robocode.Event;
import edu.commander.BehaviorCalculator;
import edu.random.Randonizable;

public abstract class Strategy implements Randonizable, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private transient AdvancedRobot advancedRobot;
	private int sucess = 1;
	private int used = 1;
	private transient boolean wasUsed = false;
	
	public Strategy(AdvancedRobot advancedRobot){
		this.advancedRobot = advancedRobot;
		prepare();
	}
	
	public void setRobot(AdvancedRobot advancedRobot){
		this.advancedRobot = advancedRobot;
		wasUsed = false;
		prepare();
	}
	
	public AdvancedRobot getAdvancedRobot(){
		return advancedRobot;
	}
	public abstract void prepare();
	public abstract void work();
	public abstract void work(Event e);
	public abstract String toString();

	public void sucess() {
		if (wasUsed){
			sucess+=1;
			System.out.println("Sucess: " + this.toString());
		}
	}

	public void failure() {
		if (wasUsed && sucess > 1){
			sucess-=1;
			System.out.println("Failure: " + this.toString());
		}
	}
	
	public void used(){
		if (!wasUsed){
			used += 1;
			wasUsed = true;
			System.out.println("Using: " + this.toString() + ".");
			System.out.println("Sucess / Failure: " + sucess + " / " + used);
		}
	}
	
	public boolean wasUsed(){
		return wasUsed;
	}
	
	@Override
	public final long getRate() {
		BehaviorCalculator bc = Factory.getInstance().getBehaviorCalculator();
		return bc.getRate(sucess, used);
	}
}
